package br.indt.framework.ui.common;

import java.util.Vector;

import br.indt.framework.log.MicrologWrapper;

public class OptionMenuScreenFullTouch implements ScreenListener{
	
	private static final MicrologWrapper log = MicrologWrapper.getLogger(OptionMenuScreenFullTouch.class);
	
	private Form form  = null;
	public static final int H_ITEM = 46;
	
	private Vector optionList = null;
	
	private boolean contextual = false;
	public boolean isContextual() {
		return contextual;
	}

	private int y = 0;

	public int getY() {
		return y;
	}

	public OptionMenuScreenFullTouch() {
		form = new Form("OptionMenuFullTouch");
		form.setMenuList(true);
		
	}

	public void show(Vector optionList) {
		form.removeAll();
		this.contextual = false;
		setScreenListener(this);
		this.optionList = optionList;
		this.form.menuListShow = true;
		form.setCurrentY(30);
		populate(optionList);
		try {
			Display.getInstance().push(form);

		} catch (Exception e) {
			//#ifdef LOGS
			log.error("[ERROR] OptionMenuScreenFullTouch:: show : " + e.getMessage());
			//#endif
		}
	}
	
	public void show(Vector optionList, int x, int y) {
		this.contextual = true;
		this.y = y;
		setScreenListener(this);
		form.removeAll();
		this.optionList = optionList;
		this.form.menuListShow = true;
		
		form.setCurrentY(y);
		populate(optionList);
		try {
			Display.getInstance().push(form);

		} catch (Exception e) {
			//#ifdef LOGS
			log.error("[ERROR] OptionMenuScreenFullTouch:: show : " + e.getMessage());
			//#endif
		}
	}

	public void populate(Vector options) {
		int count = options.size();

		for (int i = 0; i < count; i++) {
			Object opt =  options.elementAt(i);
			OptionMenuItem ci = (OptionMenuItem) opt;
			if(i == 0){
				((OptionMenuItemFullTouch)ci).setContextual(contextual);
				((OptionMenuItemFullTouch)ci).setFirstItem(true);
			}else if(i == count -1){
				((OptionMenuItemFullTouch)ci).setContextual(contextual);
				((OptionMenuItemFullTouch)ci).setLastItem(true);
			}
			form.add(ci);
		}
	}

	public boolean screenClicked(int x, int y) {
		int height = H_ITEM;

		if(contextual){
			if(optionList.size() > 1) height = height*optionList.size();

			if(y < this.y || y > this.y + height){
				Display.getInstance().pop();
				return true;
			}

		} else {
			if(y > optionList.size()*H_ITEM + 42){
				Display.getInstance().pop();
				return true;
			}
		}

		return false;
//		setScreenListener(null);
//
//		try { 
//			Display.getInstance().setHideAction(true);
//		} catch (Exception e) {
//			//#ifdef LOGS
//			Utils.log("[ERROR] OptionMenuScreenFullTouch:: screenClicked : " + e.getMessage());
//			//#endif
//		}
	}

	private void setScreenListener(ScreenListener screenListener) {
		try {
			Display.getInstance().setScreenListener(screenListener);
		} catch (Exception e) {
		e.printStackTrace();
		}
	}
	
	
}